<?php
class ModelReportAffiliate extends Model {
	public function getCommission($filter = null) {
		$sql = "SELECT at.affiliate_id, CONCAT(a.firstname, ' ', a.lastname) AS affiliate, a.email, a.status,
				SUM(at.amount) AS commission, COUNT(o.order_id) AS orders, SUM(o.total) AS total
				FROM  @@affiliate_transaction at LEFT JOIN `@@affiliate` a ON (at.affiliate_id = a.affiliate_id)
				LEFT JOIN `@@order` o ON (at.order_id = o.order_id)";

		$implode = array();

		if ($filter) {
			if ($filter->get('filter_date_start')) {
				$implode[] = "DATE(at.date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
			}
			
			if ($filter->get('filter_date_end')) {
				$implode[] = "DATE(at.date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
			}	
		}
		

		if ($implode) {
			$sql .= " WHERE " . implode(" AND ", $implode);
		}

		$sql .= " GROUP BY at.affiliate_id ORDER BY commission DESC";

		if ($filter) {
			$sql .= $filter->limit();
		}

		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalCommission($filter = null) {
		$sql = "SELECT COUNT(DISTINCT affiliate_id) AS total FROM `@@affiliate_transaction`";

		$implode = array();

		if ($filter) {
			if ($filter->get('filter_date_start')) {
				$implode[] = "DATE(date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
			}
			
			if ($filter->get('filter_date_end')) {
				$implode[] = "DATE(date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
			}	
		}

		if ($implode) {
			$sql .= " WHERE " . implode(" AND ", $implode);
		}

		return $this->db->queryOne($sql);
	}

	public function getProducts($filter = null) {
		$sql = "SELECT at.product_id, CONCAT(a.firstname, ' ', a.lastname) AS affiliate, a.email, a.status,
				SUM(at.amount) AS commission, COUNT(o.order_id) AS orders, SUM(o.total) AS total
				FROM  @@affiliate_transaction at LEFT JOIN `@@affiliate` a ON (at.affiliate_id = a.affiliate_id)
				LEFT JOIN `@@order` o ON (at.order_id = o.order_id) LEFT JOIN  @@product";

		$implode = array();

		if ($filter) {
			if ($filter->get('filter_date_start')) {
				$implode[] = "DATE(at.date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
			}
			
			if ($filter->get('filter_date_end')) {
				$implode[] = "DATE(at.date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
			}	
		}

		if ($implode) {
			$sql .= " WHERE " . implode(" AND ", $implode);
		}

		$sql .= " GROUP BY at.affiliate_id ORDER BY commission DESC";

		if ($filter) {
			$sql .= $filter->limit();
		}

		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalProducts($filter = null) {
		$sql = "SELECT COUNT(DISTINCT product_id) AS total FROM `@@affiliate_transaction`";

		$implode = array();

		if ($filter) {
			if ($filter->get('filter_date_start')) {
				$implode[] = "DATE(date_added) >= '" . ES($filter->get('filter_date_start')) . "'";
			}
			
			if ($filter->get('filter_date_end')) {
				$implode[] = "DATE(date_added) <= '" . ES($filter->get('filter_date_end')) . "'";
			}	
		}

		if ($implode) {
			$sql .= " WHERE " . implode(" AND ", $implode);
		}

		return $this->db->queryOne($sql);
	}
}
?>